// 显示中文星期格式
export const getWeekFormat = function (week) {
  let weeks = ['日', '一', '二', '三', '四', '五', '六'];
  return weeks[week]
}
// 生成用车首页时间插件显示格式
export const generatePopupTimeDate = function (currentDate) {
  let dates = []
  let hours = []
  let minutes = []
  // 需要生成多少天
  const TOTAL_DAYS = 7
  for (let i = 0; i < TOTAL_DAYS; i++) {
    let d = moment(new Date).add(i, 'days')
    let _year = d.year()
    let _month = (d.month()) < 10 ? '0' + (d.month() + 1) : d.month() + 1
    let _day = i < 10 ? '0' + i : i
    dates.push(
      `${_year}年 ${_month}月 ${d.format('DD')}日 星期${getWeekFormat(d.day())}`
    )
  }
  // 生成单列小时数数据
  for (let i = 0; i < 24; i++) {
    let _hour = i < 10 ? '0' + i : i
    hours.push(`${_hour}点`)
  }
  // 生成单列分钟数数据
  for (let i = 0; i < 60; i++) {
    let _minute = i < 10 ? '0' + i : i
    // 如果等于当前分钟, 则默认显示
    minutes.push(`${_minute}分`)
  }
  return [dates, hours, minutes]
};
// 将当前日期处理成 组件需要的格式 e.g： ["01月 01日 星期一", "00点", "00分"]
export const formatCurrentDateInit = function (currentDate) {
  let d = moment(currentDate);
  let _year = d.year();
  let _month = (d.month()) < 10 ? '0' + (d.month() + 1) : d.month() + 1;
  let _day = d.get('date') < 10 ? '0' + d.get('date') : d.get('date');
  let date = `${_year}年 ${_month}月 ${_day}日 星期${getWeekFormat(moment(`${d.year()}-${_month}-${_day}`).day())}`;
  let hour = `${d.hour() < 10 ? '0' + d.hour() : d.hour()}点`;
  let minute = `${d.minute() < 10 ? '0' + d.minute() : d.minute()}分`;
  return [date, hour, minute];
};

/**
 *
 * @desc   判断`obj`是否为空
 * @param  {Object} obj
 * @return {Boolean}
 */
export const isEmptyObject =  function (obj) {
  if (!obj || typeof obj !== 'object' || Array.isArray(obj))
    return false
  return !Object.keys(obj).length
}

// 设置或者获取元素data属性
export const getData = function  (el, name, val) {
  const prefix = 'data-'
  name = prefix + name
  if (val) {
    return el.setAttribute(name, val)
  } else {
    return el.getAttribute(name)
  }
}

export const sortCityList = function (data) {
  const HOT_CITY = '热门城市';
  let map = {
    hot: {
      title: HOT_CITY,
      items: []
    }
  };
  data.cityList.forEach((_item, _index) => {
    let key = _item.pinyin.slice(0, 1).toUpperCase();
    if (!map[key]) {
      map[key] = {
        title: key,
        items: []
      }
    }
    map[key].items.push({
      cityId: _item.cityId,
      name: _item.name
    })
  })
  data.hotCity.forEach((_item, _index) => {
    map.hot.items.push({
      cityId: _item.cityId,
      name: _item.name
    })
  })
  // 排序数据
  let hot = []
  let ret = []
  for (let key in map) {
    let val = map[key]
    if (val.title.match(/[a-zA-Z]/)) {
      ret.push(map[key])
    } else if (val.title === HOT_CITY) {
      hot.push(val)
    }
  }
  ret.sort((a, b) => {
    return a.title.charCodeAt(0) - b.title.charCodeAt(0)
  })
  return hot.concat(ret)
};

export const deepClone = function (values) {
  var copy;

  // Handle the 3 simple types, and null or undefined
  if (null == values || "object" != typeof values) return values;

  // Handle Date
  if (values instanceof Date) {
    copy = new Date();
    copy.setTime(values.getTime());
    return copy;
  }

  // Handle Array
  if (values instanceof Array) {
    copy = [];
    for (var i = 0, len = values.length; i < len; i++) {
      copy[i] = deepClone(values[i]);
    }
    return copy;
  }

  // Handle Object
  if (values instanceof Object) {
    copy = {};
    for (var attr in values) {
      if (values.hasOwnProperty(attr)) copy[attr] = deepClone(values[attr]);
    }
    return copy;
  }

  throw new Error("Unable to copy values! Its type isn't supported.");
}

export const arrayEqual = function (arr1, arr2) {
  if (arr1 === arr2) return true;
  if (arr1.length != arr2.length) return false;
  for (var i = 0; i < arr1.length; ++i) {
    if (arr1[i] !== arr2[i]) return false;
  }
  return true;
};

// 去除数组中的undefined
Array.prototype.notempty = function(){
  return this.filter(t => t !== undefined && t !== null);
}

